import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:frametrim/utility/screen.dart';
import 'package:flutter_swiper/flutter_swiper.dart';

/*轮播图
 *flutter_swiper : 1.1.6
 */
class BannerView extends StatelessWidget {
  /*轮播图数据源*/
  late List<Image> images;

  late Function(int) mFunction;

  BannerView(this.images, this.mFunction);

  @override
  Widget build(BuildContext context) {
    var width = Screen.width - 20;
    return Container(
      margin: EdgeInsets.fromLTRB(10, 10, 10, 0),
      height: width / 175 * 100,
      child: Swiper(
        scrollDirection: Axis.horizontal,
        // 方向 Axis.horizontal
        itemBuilder: (context, index) {
          return images[index];
        },
        //不要用这个兴致，会报错
        // indicatorLayout: PageIndicatorLayout.COLOR,
        //条目个数
        itemCount: images.length,
        //自动翻页
        autoplay: true,
        //动画时间
        duration: 800,
        // 视窗比例
        // viewportFraction: 0.8,
        itemWidth: width - 10,
        //页面切换间隔，默认3000ms
        autoplayDelay: 5000,
        //分页指示
        pagination: buildSwiperPagintion(),
        //点击事件
        onTap: (index) {
          print('点击的第${index}个');
          mFunction(index);
        },
      ),
    );
  }

  /*指示器*/
  buildSwiperPagintion() {
    return SwiperPagination(
      //指示器显示的位置
      alignment: Alignment.bottomCenter, // 位置 Alignment.bottomCenter 底部中间
      // 距离调整
      margin: const EdgeInsets.fromLTRB(0, 0, 0, 5),
      // 指示器构建
      builder: DotSwiperPaginationBuilder(
          // 点之间的间隔
          space: 3,
          // // 没选中时的大小
          size: 8,
          // // 选中时的大小
          activeSize: 8,
          // 没选中时的颜色
          color: Colors.white.withOpacity(0.5),
          //选中时的颜色
          activeColor: Colors.white),
    );
  }
}
